package com.yhcnet.blog.controller;

import com.yhcnet.blog.pojo.Draft;
import com.yhcnet.blog.reponse.DraftBody;
import com.yhcnet.blog.reponse.Resp;
import com.yhcnet.blog.service.DraftService;
import com.yhcnet.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/draft")
@CrossOrigin
@RestController
public class DraftController {
    @Autowired
    DraftService draftService;

    @Autowired
    UserService userService;

    @RequestMapping("/create")
    public Resp create(@RequestBody DraftBody draftBody){
        if(draftBody == null) return Resp.loginFail();
        if(draftService.create(draftBody)) return Resp.OK();
        return Resp.loginFail();
    }

    @RequestMapping("/getD")//参数 uuid+email
    public Resp getD(@RequestBody DraftBody draftBody){
        if(draftBody == null) return Resp.loginFail();
        String uuid = draftBody.getUuid();
        String email = draftBody.getEmail();
        boolean judge = userService.judge(uuid, email);
        if(judge){
            Draft d = draftService.getD(email);
            if(d != null){
                return Resp.OK(d);
            }
        }
        return Resp.loginFail();
    }

    @RequestMapping("/deleteD")
    public Resp deleteD(String email){
        if(draftService.delete(email)) return Resp.OK();
        return Resp.loginFail();
    }
}
